#ifndef FILEANALYSIS_H
#define FILEANALYSIS_H


#include "stdlib.h"
#include "stdio.h"
#include <QDebug>
#include <QMap>
#include <QTextCodec>
#include <QString>
#include <QFile>
#include <QUrl>
#include <Utils.h>


typedef struct ID3V2FrameH
{
   char frameID[4];               //存放标签格式，是否为id3v2
   unsigned char size[4];         //存放标签数据的大小
   char flags[2];
}ID3V2FH;

typedef struct ID3V1Frame
{
    char Header[3];     //标签头
    char Title[30];     //标题
    char Artist[30];    //艺术家
    char Album[30];     //专辑
    char Year[4];       //出品年代
    char Comment[30];   //备注
    char Cenre[1];      //类型
} ID3V1Frame;

typedef struct frameIDStruct
{
    int beginNum;
    int endNum;
    QString FrameId;

}frameIDStruct;

typedef struct FrameStruct
{
    char FrameID[4];            /*用四个字符标识一个帧*/
    unsigned char Size[4];      /*帧内容的大小,不包括帧头,不得小于1*/
    char Flags[2];              /*存放标志*/
}Frame;

class MusicFileInfo
{
public:
    MusicFileInfo(){id = 0; Time = 0;};
    MusicFileInfo(int ID, QString name, QString singer, QString album,  QString url, int time):
        id(ID), Name(name), Singer(singer), Album(album), Url(url), Time(time) {};

    MusicFileInfo(MusicFileInfo* mf):
        id(mf->id), Name(mf->Name), Singer(mf->Singer), Album(mf->Album), Url(mf->Url), Time(mf->Time) {};

    void printInfo();
    bool readPic();
    MusicFileInfo* copy()
    {
        MusicFileInfo* new_object = new MusicFileInfo(this);
        new_object->File_name = File_name;
        new_object->Picture_url = Picture_url;
        new_object->Picture_type = Picture_type;
        new_object->pic_flag = pic_flag;

        return new_object;
    }

public:
    QString Url;            //存放这首歌的详细地址
    QString Name;            //歌名  TIT2
    QString Album;           //专辑  TALB
    QString Singer;          //歌手  TPE1
    QString Picture_url;      //歌曲图片存放路径
    QString Picture_type;     //图片类型 jpg,png
    int Time;           //歌曲时长 ms
    QString File_name;

    int     id;                //歌曲编号
    bool    pic_flag;         //是否有图片
};


class MusicFile
{
public:
    MusicFile(){};
    virtual MusicFileInfo* GetAllInfo(const wchar_t *url, int songNumber) = 0;

public:
    QString m_url;

};

class MP3File : public MusicFile
{
public:
    MP3File(){};
    MusicFileInfo* GetAllInfo(const wchar_t *url, int songNumber);

private:
    int getBitrate(int version_id, int layer_index, int bit_index);
    int getSideInfoSize(int version_id, int layer_index, int channel_index);
    int getTime(FILE* fp, int ID3V2_len);

public:
    unsigned char Header[3];
    unsigned char FrameId[4];     //存放帧标识
    unsigned char Header_size[4];
    unsigned int mp3_TagSize;
    unsigned char frameSize[4];      //存放该帧内容的大小
    unsigned int framecount;          //计算出帧内容的大小

    int bitrate_table[6][15] = {
        {-1,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
                {-1,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
                {-1,32,40,48,56,64,80,96,112,128,160,192,224,256,320},
                {-1,32,48,56,64,80,96,112,128,144,160,176,192,224,256},
                {-1,8,16,24,32,64,80,56,64,128,160,112,128,256,320},
                {-1,8,16,24,32,64,80,56,64,128,160,112,128,256,320}};

    QString channel[4] = {
            "立体声", "混合立体声", "双声道", "单声道"
        };

    int sampling_per_flame_table[4][3] = {
            {384, 1152, 576},
            {-1, -1, -1},
            {384, 1152, 576},
            {384, 1152, 1152}
        };

    int samplingrate_table[4][3] = {
            {11025, 12000, 8000},
            {-1, -1, -1},
            {22050, 24000, 16000},
            {44100, 48000, 32000}
        };

        //边信息大小
    int side_info[2][2] = {
            {32, 17},
            {17, 9}
        };
};

#endif // FILEANALYSIS_H
